package my.study.org.mongodb.examples;

import java.net.UnknownHostException;

import my.study.org.mongodb.constant.MongoDBConstant;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.DBObject;
import com.mongodb.Mongo;
import com.mongodb.MongoException;

public class DocUpdateExample {

	private final static Log logger = LogFactory.getLog(DocUpdateExample.class);

	/**
	 * 文档替换
	 * 
	 * @param args
	 * @throws MongoException
	 * @throws UnknownHostException
	 * @throws InterruptedException 
	 */
	public static void main(String[] args) throws UnknownHostException,
			MongoException, InterruptedException {
		updateByReplaceDoc(102);
		//updateBySetter(102);
	}

	public static void updateByReplaceDoc(int size) throws UnknownHostException, MongoException, InterruptedException {
		Mongo mongo = new Mongo(MongoDBConstant.MONGO_HOST,
				MongoDBConstant.MONGO_PORT);

		logger.info(mongo.getDatabaseNames());
		DB db = mongo.getDB("DocUpdateExample");
		db.dropDatabase();
		DBCollection coll = db.getCollection("user");
		logger.info("begin to insert");
		
		long beginTime = System.currentTimeMillis();
		DBObject[] docs = new DBObject[(int) size];
		for (int i = 0; i < size; i++) {
			DBObject doc = new BasicDBObject();
			doc.put("name", "fanhongjie" + i);
			doc.put("age", i);
			doc.put("address", "address" + i);
			doc.put("phonenumber", "phonenumber" + i);
			docs[i] = doc;
		}
		logger.info("finish get data");
		coll.insert(docs);
		logger.info("end insert takes "
				+ (System.currentTimeMillis() - beginTime) + "ms");
		beginTime = System.currentTimeMillis();
		
		DBCursor cursor = null;

		cursor = coll.find();
		int time = 0;
		while (cursor.hasNext()) {
			
			DBObject doc = cursor.next();
			System.out.println(time++ + " " + doc);
			DBObject queryDoc = new BasicDBObject();
			queryDoc.put("_id", doc.get("_id"));
			queryDoc.put("name", doc.get("name"));
			
			DBObject subDoc = new BasicDBObject();
			subDoc.put("address", doc.get("address"));
			subDoc.put("phonenumber", doc.get("phonenumber"));
			doc.put("info", subDoc);
			doc.put("username", doc.get("name"));
			doc.put("_id", doc.get("_id"));
			doc.removeField("name");
			doc.removeField("address");
			doc.removeField("phonenumber");
			
			//logger.info("queryDoc:"+ queryDoc + " updateDoc:" + doc);
			coll.update(queryDoc, doc);
			Thread.sleep(100);
		}
		logger.info("end update takes "
				+ (System.currentTimeMillis() - beginTime) + "ms");
	}

	
	public static void updateBySetter(int size) throws UnknownHostException, MongoException {
		Mongo mongo = new Mongo(MongoDBConstant.MONGO_HOST,
				MongoDBConstant.MONGO_PORT);

		logger.info(mongo.getDatabaseNames());
		DB db = mongo.getDB("DocUpdateExample");
		db.dropDatabase();
		DBCollection coll = db.getCollection("user");
		long beginTime = System.currentTimeMillis();
		logger.info("begin to insert");
		for (int i = 0; i < size; i++) {
			DBObject doc = new BasicDBObject();
			doc.put("name", "fanhongjie" + i);
			doc.put("age", i);
			doc.put("address", "address" + i);
			doc.put("phonenumber", "phonenumber" + i);
			coll.insert(doc);
		}

		DBCursor cursor = null;
//		cursor = coll.find();
//		while (cursor.hasNext()) {
//			logger.info(cursor.next());
//		}

		cursor = coll.find();
		int time = 0;
		while (cursor.hasNext()) {
			
			DBObject doc = cursor.next();
			System.out.println(time++ + " " + doc);
			DBObject modifier = new BasicDBObject();
			DBObject setterDoc = new BasicDBObject();
			DBObject unsetterDoc = new BasicDBObject();
			modifier.put("$set", setterDoc);
			modifier.put("$unset", unsetterDoc);
			
			DBObject queryDoc = new BasicDBObject();
			queryDoc.put("_id", doc.get("_id"));
			
			DBObject subDoc = new BasicDBObject();
			subDoc.put("address", doc.get("address"));
			subDoc.put("phonenumber", doc.get("phonenumber"));

			setterDoc.put("info", subDoc);
			setterDoc.put("username", doc.get("name"));
			unsetterDoc.put("name", 0);
			unsetterDoc.put("address", 0);
			unsetterDoc.put("phonenumber", 0);
			
			//logger.info("doc:" + doc + " queryDoc:"+ queryDoc + " modifier:" + modifier);
			coll.update(queryDoc, modifier);
			//System.out.println("Last error : " + db.getLastError());
		}
		logger.info("end update takes "
				+ (System.currentTimeMillis() - beginTime) + "ms");
//		cursor = coll.find();
//		while (cursor.hasNext()) {
//			logger.info("After UpdateBySetter" + cursor.next());
//		}
	}

}
